// pages/home/appointment/appointment.js
const api = require('../../../config/api.js');
const https = require('../../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    stores_id: '',
    stores_name: '杭州店'
  },

  // 保存
  async formSubmit(e) {
    let that = this;
    wx.showLoading();
    let formData = e.detail.value;
    console.log('form发生了submit事件，携带数据为：', formData);
    // 验证
    if (that.validateForm(formData)) {
      // 保存
      let params = {
        openid: wx.getStorageSync('openid'),
        name: formData.name,
        stores_id: that.data.stores_id,
        iphone: formData.iphone
      }
      https.request(api.Appointment, params, 'post').then(res => {
        wx.hideLoading()
        if (res.error_code == 0) {
          // 预约成功
          wx.showModal({
            title: '提示',
            content: '预约成功，请等待门店联系您！',
            showCancel: false,
            confirmText: '已知晓',
            success(res) {
              wx.switchTab({
                url: '/pages/home/home',
              })
            }
          })
        }
      }).catch(err => {
        wx.hideLoading()
        console.log('错误', err)
      })

    } else {}


  },
  validateForm(formData) {
    let that = this;
    // let formData = that.data.userInfo;
    let rstMsg = "";
    if (formData.name.length == 0) {
      rstMsg = '名字不能为空';
    } else if (formData.iphone.length == 0) {
      rstMsg = '手机号不能为空';
    } else if (formData.iphone.length != 11) {
      rstMsg = '手机号格式错误';
    }
    if (rstMsg != '') {
      wx.showToast({
        icon: 'none',
        title: rstMsg,
      })
      return false;
    }
    return true;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    that.setData({
      stores_id: options.id,
      stores_name: options.name,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})